<?php
require_once("includes/pagetop.php");

if(isset($Email_ID) && isset($First_Name))
{
    $insertarr=array("Full_Name"=>$First_Name." ".$Last_Name);
    $insertarr["Last_Name"]=$Last_Name;
    $insertarr["Email_ID"]=$Email_ID;
    
    $mailidarr=explode("@",$Email_ID);
    $Allyfoliouserid=$mailidarr[0];
    
    $checkforid=$pdodbobj->fetch_array($pdodbobj->query("select Allyfolio_User_ID from af_users where Allyfolio_User_ID like :allyfolioid order by User_ID desc",array("allyfolioid"=>"%".$Allyfoliouserid."%")));
    
    if(count($checkforid)>0)
    {
        $allyfolioid=str_ireplace("@allyfolio.in","",$checkforid[0]['Allyfolio_User_ID']);
        $existmailidarr=explode("_",$allyfolioid);
        if(count($existmailidarr)>1)
        {
            $Allyfoliouserid=$mailidarr[0]."_".(intval($existmailidarr[1])+1)."@allyfolio.in";
        }
        else
        {
            $Allyfoliouserid=$mailidarr[0]."_1@allyfolio.in";
        }
        
    }
    else
    {
        $Allyfoliouserid.="@allyfolio.in";
    }
    
    $insertarr['Allyfolio_User_ID']=$Allyfoliouserid;
    
    $insertarr["Password"]=md5(trim($Password));
    $insertarr["Gender"]=$Gender;
    $insertarr["Registration_Date"]=time();
    $randomstring=random_string(20);
    $insertarr["Verify_Random_String"]=$randomstring;
    $insertarr["DOB"]=mktime(0,0,1,$Month,$Day,$Year);
    $insertarr=sanitize($insertarr);
    
    require_once"includes/mailscript.php";
    
    $subject="Welcome to Allyfolio Social Portal.";
    
    $message="
    <table cellspacing='2' cellpadding='2' width='100%'>
        <tr>
            <td>
            Hi ".$First_Name." ".$Last_Name.",<br/><br></td>
        </tr>
        <tr>
            <td>
            
                You have registered your account with us. Your Account credentials are mentioned below:
             </td>
        </tr>
        <tr>
            <td>
            Username : ".$Email_ID."
            </td>    
        </tr>
        <tr>
            <td>
            Password : ".$Password."
            </td>    
        </tr> 
        <tr>
            <td>
            Please click on below link to activate your account & continue with Allyfolio.<br>
            <a href='".WEBURL."activateacnt.php?actcode=".$randomstring."' target='_blank'>".WEBURL."activateacnt.php?actcode=".$randomstring."</a>
            </td>    
        </tr>
        <tr>
            <td>
            <br/>
            <br/>
               Thanks,<br/>Allyfolio Support Team.
            </td>
        </tr>       
    </table>" ;  
    
    $pdodbobj->insertData("af_users",$insertarr);
    if($pdodbobj->affectedrows>0)
    {
        if(sendMail($Email_ID,$subject,$message,$headers))
        {
            $successmessage="Your Account is created and sent an activation email to your email id. Please activate your account first to login.";
        }
        else
        {
            $successmessage="Your Account is created and activation email cannot be sent due to any reason. Please contact to support team for activating your account.";
        }
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AllyFolio | SignUp </title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"  />

<script src="js/general.js"></script>
<script src="js/functions.js"></script>

<link rel="stylesheet" href="alertify/themes/alertify.core.css" />
<link rel="stylesheet" href="alertify/themes/alertify.default.css" id="toggleCSS" />
<script src="alertify/lib/alertify.min.js"></script>	
<script src="js/jquery1.10.2.min.js">
</script>
<script src="js/hello.js"></script>
<script>
//<![CDATA[
$(document).ready(function(){
  $(".drop_btn").click(function(){
    $(".showbox").toggle();
  });
  
  if(!$.support.placeholder) { 
		var active = document.activeElement;
		$(':text').focus(function () {
			if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
				$(this).val('').removeClass('hasPlaceholder');
			}
		}).blur(function () {
			if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
				$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder').css('color','#cccccc');
			}
		});
		$(':text').blur();
		$(active).focus();
		$('form').submit(function () {
			$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
		});
	}
});

//]]>
</script>
</head>

<body>

	<div id="main_wrapper">
    <!--=================Header Start=======================-->
	<?php
    
    //$pageheading="Sign Up";
    require_once("includes/header.php");
    
    //$_SESSION['captcha']=random_string(6);
    
    ?>
    <!--=================Body Contain Start=======================-->
    <div class="wrapper">
                <?php
                if(isset($successmessage) && $successmessage!="")
                {
                ?>
                    <script>
                        alertify.alert("<?php echo $successmessage; ?>");
                    </script>
                
                <?php
                }
                ?>
    	<div class="header">
        	<div class="signup_wrap">
            	<div class="abc">Sign Up For <img src="images/small_logo.png" alt="logo" width="126" height="30" style="position:relative; top:8px;"  /></div>
       		 <div class="signup-area" style="margin-top:20px">
              <form name="reg" method="post">
        	<div class="registration_redesign">
           
        		<div class="user_row1">
                
                <div class="first_user">                        	
					<input class="inputtext_signup-area signupinput" type="text" title="First Name" placeholder="First Name" id="fname" name="First_Name" >
                    </div>                        
                    <div class="Lst_name">                        
					<input class="inputtext_signup-area_2" type="text" title="Last Name" placeholder="Last Name" id="lname" name="Last_Name" >
                     </div>
                </div>
	
            	 <div class="our_mail">
				   <input class="inputtext_signup-area_3 signupinput" type="text" name="Email_ID" title="Your E-mail Please" maxlength="50" placeholder="Your E-mail Please" id="emailid" >
				</div>
                <div class="our_mail">
				   <input class="inputtext_signup-area_3" type="password" name="Password" placeholder="Password" maxlength="16" title="New Password" id="pwd" >
				</div>
                <div class="our_mail">
				   <input class="inputtext_signup-area_3" type="password" title="Re-Enter Password" maxlength="16" placeholder="Re-Enter Password" id="cpwd" >
				</div>
                
                <div class="_dob">
                    <div class="lable-dob">Date of Birth</div>
                    <select id="day" class="_5dba" name="Day" title="Day">
                    <option selected="1" value="0">Day</option>
                    <?php
                    for($i=1;$i<=31;$i++)
                    {
                    ?>
                    <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                    <?php
                    }
                    ?>
                    </select>
                    <select class="_5dba" name="Month" id="month" title="Month">
                    <option selected="1" value="0">Month</option>
                    <option value="1">Jan</option>
                    <option value="2">Feb</option>
                    <option value="3">Mar</option>
                    <option value="4">Apr</option>
                    <option value="5">May</option>
                    <option value="6">Jun</option>
                    <option value="7">Jul</option>
                    <option value="8">Aug</option>
                    <option value="9">Sep</option>
                    <option value="10">Oct</option>
                    <option value="11">Nov</option>
                    <option value="12">Dec</option>
                    </select>
                    <select id="year" class="_5dba" name="Year" title="Year">
                    <option selected="1" value="0">Year</option>
                    <?php 
                    for($i=2014;$i>1950;$i--)
                    {
                        ?>
                        <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                        <?php
                    }
                    ?>
                    </select>
                    
                </div>
                    
                <div class="gander">
                    <span class="_5k_3" data-name="gender_wrapper" data-type="radio">
                    <span class="_5k_2 _5dba">
                    <input type="radio" value="Female" name="Gender" id="genderf">
                    <label class="_58mt" >Female</label>
                    </span>
                    <span class="_5k_2 _5dba">
                    <input type="radio" value="Male" name="Gender" id="genderm">
                    <label class="_58mt" >Male</label>
                    </span>
                    </span>
                    
                </div>
            </div>
            <div class="signup_with">
            	<h3>Save Time By Using Your Other Accounts</h3>
                <div class="drop_btn">Sign  up with</div>
            	<img src="images/star.png"  style="float:right;position: relative;
                right: 73px;
                top: -34px;"/>
                
                <div class="showbox">
                	<center>  <p style="padding-top: 10px;">
                        <a href="javascript:void(0);" onclick="loginsocial('facebook');"><img src="images/signupwithfacebook.jpg" width="150px" height="40px" /></a>
                    </p>
                    <br />
                    <p>
                        <a href="javascript:void(0);" onclick="loginsocial('google');"><img src="images/signupwithgoogle.jpg" width="150px" height="40px" /></a>
                    </p>
                    </center>
                </div>
            
            </div>
            <div class="argee">
                <input type="radio" value="1" name="termsandcondition" id="terms">
                <label class="_58mt">I agree <a target="_blank" href="terms_conditions.php">Terms & Conditions</a> whice are given by this site</label>
            </div>
            <div class="captcha">
                
               <table cellpadding="2" width="100%" border="0" cellspacing="2">
					<tr>
						<td><img src="images/refresh.png" width="40px" height="40px" onclick="change_captcha();" style="cursor:pointer"></td>
                        <td>
                            <div class="cap1">                        	
                                <img id="imgCaptcha" src="create_captcha.php" /> 
                            </div>
                        </td>     
                        <td>
                            <div class="cap2">                        
						      <input class="inputtext_signup-area_2" type="text" value="" maxlength="6" placeholder="Captcha" id="captcha"  />
                            </div>    
                        </td>                    
                    </tr>
              </table>
                    
            </div>	
            <div class="clearfix"></div>  
        
            <div class="btn_submit">          
                <button type="button" id="signupbtn">Sign Up </button> &nbsp;&nbsp;<img src="images/loading-bar.gif" id="loadingbar" style="display: none;" />
            </div>	
            <div class="clearfix"></div>  
		      <div class="_58mt" style="margin:35px auto 10px 163px ">Institutes, Brand, Business, Ads, Celebrity<span class="drop_btn2" id="celebsignup" style="cursor: pointer;">Sign Up Here</span></div>

        </form>
        <form name="signupsocialform" method="post">
            <input type="hidden" id="verfid" name="verfid" />
        </form>
    </div>
    </div>
    </div>
    </div>
            
            
		</div>
    </div>
    <!--<input type="hidden" id="captcha"  value="<?php //echo $_SESSION['captcha'] ?>"/>-->
    <div class="clearfix"></div>
    <!--=================Body Contain end=======================-->
    </div>
<?php
    require_once("includes/footer.php");
?>
<script>
var loginusing='';
var requestlogin='';

var loginthrough="";
var firstname="";
var lastname="";
var emailid="";
var loginverifiedid=0;
var fullname="";
var gender="";

hello('facebook').logout();
hello('google').logout();


function loginsocial(networkid)
{
    requestlogin=networkid;
    if(loginusing!='')
    {
        hello(loginusing).logout();
    }
    else
    {
        loginusing=networkid;
        hello( networkid ).login();    
    }
    
}

hello.init({ 
	facebook : '660506994019141',
    google: '488492302346-cmim1i50b51gebb9kb4plquidi9nm55k.apps.googleusercontent.com'
},{scope:'email'});

hello.on('auth.login', function(auth){
    
    requestlogin='';
	if(auth.network=='google')
    {
        hello(  auth.network  ).api( '/me' ).success(function(r){
    	    
            loginthrough="Google";
            firstname=r.first_name;
            lastname=r.last_name;
            emailid=r.email;
            loginverifiedid=r.id;
            fullname=r.name;
            gender=r.gender;
            
            
            $.ajax({
              async:false,
        	  url: "ajax_search/checksignup.php?via="+loginthrough+"&fname="+firstname+"&lname="+lastname+"&gender="+gender+"&emailid="+emailid+"&vid="+loginverifiedid+"&fullname="+fullname,
        	  success: function( data ) {
        	    if(data=="100100")
                {
                    //document.getElementById('verfid').value=loginverifiedid;
                    document.signupsocialform.action='socialpassword.php?t='+loginverifiedid;
       	            document.signupsocialform.submit();
                }
                else if(data=="000100")
                {
                    alertify.alert("There is already an account exists for this email id.");
                }
                else
                {
                    alertify.alert("There is some problems in signup using google.");
                    console.log(data);
                }
        	  }
        	});
    	});
     }
     else if(auth.network=='facebook')
     {
        hello(  auth.network  ).api( '/me' ).success(function(r){
    	    
            loginthrough="Facebook";
            firstname=r.first_name;
            lastname=r.last_name;
            emailid=r.email;
            loginverifiedid=r.id;
            fullname=r.name;
            gender=r.gender;
            
            $.ajax({
              async:false,
        	  url: "ajax_search/checksignup.php?via="+loginthrough+"&fname="+firstname+"&lname="+lastname+"&gender="+gender+"&emailid="+emailid+"&vid="+loginverifiedid+"&fullname="+fullname,
        	  success: function( data ) {
        	    if(data=="100100")
                {
                    //document.getElementById('verfid').value=loginverifiedid;
                    document.signupsocialform.action='socialpassword.php?t='+loginverifiedid;
       	            document.signupsocialform.submit();
                }
                else if(data=="000100")
                {
                    alertify.alert("There is already an account exists for this email id.");
                }
                else
                {
                    alertify.alert("There is some problems in signup using facebook.");
                    console.log(data);
                }
        	  }
        	});
                   
    	});
     }
     
     
});

hello.on('auth.logout', function(auth){
    loginusing='';
    if(requestlogin!='')
    {
        hello(requestlogin).login();
    }
});

</script>
<script lang="javascript">
//<![CDATA[
    
    $("#signupbtn").click(validateUserInput);
    
    $("#celebsignup").click(function(){
        window.location='signup_brand.php';
      });
    
    function change_captcha()
    {
    	var img = document.getElementById('imgCaptcha'); 
       //Change the image
        img.src = 'create_captcha.php?' + Math.random();
    }
    
    function validateUserInput()
    {
        
        if(trim(document.getElementById('fname').value)=="")
        {
            alertify.alert("Please enter First Name.");
            document.getElementById('fname').focus();
            return false;
        }
        else if(trim(document.getElementById('lname').value)=="")
        {
            alertify.alert("Please enter Last Name.");
            document.getElementById('lname').focus();
            return false;
        }
        else if(trim(document.getElementById('emailid').value)=="")
        {
            alertify.alert("Please enter enter email id.");
            document.getElementById('emailid').focus();
            return false;
        }
        else if(!emailpattern.test(trim(document.getElementById('emailid').value)))
        {
            alertify.alert("Please enter valid email id.");
            document.getElementById('emailid').focus();
            return false;
        }
        else if(trim(document.getElementById('pwd').value)=="")
        {
            alertify.alert("Please enter password.");
            document.getElementById('pwd').focus();
            return false;
        }
        else if(trim(document.getElementById('pwd').value).length<8)
        {
            alertify.alert("Password length should be at least 8 characters.");
            document.getElementById('pwd').focus();
            return false;
        }
        else if(!checkPasswordComplexity(trim(document.getElementById('pwd').value)))
        {
            alertify.alert("Password should be alphanumeric.");
            document.getElementById('pwd').focus();
            return false;
        }
        else if(trim(document.getElementById('cpwd').value)!=trim(document.getElementById('pwd').value))
        {
            alertify.alert("Confirm Password Mismatched.");
            document.getElementById('cpwd').focus();
            return false;
        }
        else if(trim(document.getElementById('day').value)=="0")
        {
            alertify.alert("Please select day of birthday.");
            document.getElementById('day').focus();
            return false;
        }
        else if(trim(document.getElementById('month').value)=="0")
        {
            alertify.alert("Please select month of the birthday.");
            document.getElementById('month').focus();
            return false;
        }
        else if(trim(document.getElementById('year').value)=="0")
        {
            alertify.alert("Please select year of the birthday.");
            document.getElementById('year').focus();
            return false;
        }
        else if(!document.getElementById('genderf').checked && !document.getElementById('genderm').checked)
        {
            alertify.alert("Please select gender");
            return false;
        }
        else if(!document.getElementById('terms').checked)
        {
            alertify.alert("Please check that you are agree with our terms and conditions.");
            return false;
        }
        else if(trim(document.getElementById('captcha').value)=="" || trim(document.getElementById('captcha').value)=="Captcha")
        {
            alertify.alert("Please enter captch value.");
            document.getElementById('captcha').focus();
            return false;
        }
        else
        {
            document.getElementById('loadingbar').style.display='';
            
            $.ajax({
              async:false,
        	  url: "ajax_search/validateinputs.php?day="+trim(document.getElementById('day').value)+"&month="+trim(document.getElementById('month').value)+"&year="+trim(document.getElementById('year').value)+"&captcha="+trim(document.getElementById('captcha').value)+"&Email_ID="+trim(document.getElementById('emailid').value),
        	  success: function( data ) {
        	    document.getElementById('loadingbar').style.display='none';
   	            if(data=="Already Exists Emailid")
                {
                    alertify.alert("This Email Id is already registered with us.");
                    return false;
                }
                else if(data=="Invalid Date")
                {
                    alertify.alert("This is not a valid date of birth.");
                    return false;
                }
                else if(data=="Captcha Mismatched")
                {
                    alertify.alert("Captcha did not matched.");
                    return false;
                }
                else if(data=="Success")
                {
                    document.reg.submit();
                }
                
        	  }
        	});
            document.getElementById('loadingbar').style.display='none';
        }
    }
    
    function checkInput()
    {
        document.getElementById('loadingbar').style.display='none';
        $.ajax({
                async:false,
        	  url: "ajax_search/validateinputs.php?day="+trim(document.getElementById('day').value)+"&month="+trim(document.getElementById('month').value)+"&year="+trim(document.getElementById('year').value)+"&captcha="+trim(document.getElementById('captcha').value)+"&Email_ID="+trim(document.getElementById('emailid').value),
        	  success: function( data ) {
        	    
   	            if(data=="Already Exists Emailid")
                {
                    alertify.alert("This Email Id is already registered with us.");
                    return false;
                }
                else if(data=="Invalid Date")
                {
                    alertify.alert("This is not a valid date of birth.");
                    return false;
                }
                else if(data=="Captcha Mismatched")
                {
                    alertify.alert("Captcha did not matched.");
                    return false;
                }
                else if(data=="Success")
                {
                    document.reg.submit();
                }
                
        	  }
        	});
    }
    
//]]>

</script>